/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* ans=nullptr;
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        dfsBST(root,val);
        return ans;
    }
    void dfsBST(TreeNode* root,int val){
        if(root==nullptr){return;}
        if(root->val==val){ans=root;return;}
        if(val<root->val){dfsBST(root->left,val);}
        else if(val>root->val){dfsBST(root->right,val);}
        return;
    }
};